package cn.stylefeng.guns.modular.rest.hxStorageStock.controller;

import cn.hutool.http.HttpRequest;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.entity.BasicPosition;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicCustomerParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicXlbOauth2tokenParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCustomerResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicXlbOauth2tokenResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.basic.service.BasicXlbOauth2tokenService;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStorageMoveRecord;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageMoveRecordParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageStockParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStorageMoveRecordResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageMoveRecordService;
import cn.stylefeng.guns.modular.hx.storage.service.HxStorageStockService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.rest.hxStorageStock.vo.StorageMoveCntVo;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.api.R;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;


/**
 * 移库记录表控制器
 *
 * @author
 * @Date 2023-09-09 11:20:46
 */
@Controller
@RequestMapping("/rest/hxStorageMoveRecord")
public class HxStorageMoveRecordController extends BaseController {
    @Autowired
    private HxStorageMoveRecordService hxStorageMoveRecordService;
    @Autowired
    private HxStorageStockService hxStorageStockService;
    @Autowired
    private UserAop userAop;
    @Autowired
    private BasicXlbOauth2tokenService basicXlbOauth2tokenService;

    @Autowired
    private UserService userService;
    @Autowired
    private BasicAreaService basicAreaService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicCustomerService basicCustomerService;


    /**
     * 查询列表
     *
     * @author
     * @Date 2023-09-09
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxStorageMoveRecordParam hxStorageMoveRecordParam, String condition) {
        User user = userAop.getUser();
        hxStorageMoveRecordParam.setDeptId(user.getDeptId());
        hxStorageMoveRecordParam.setTransferUserId(user.getUserId());
        return this.hxStorageMoveRecordService.findPageBySpec(hxStorageMoveRecordParam, user.getOperateId(), condition);
    }

    /**
     * 移库
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/remove")
    public ResponseData removeStorage(HxStorageMoveRecordParam hxStorageMoveRecordParam, Long[] ids) {
        if (ToolUtil.isEmpty(hxStorageMoveRecordParam) || ToolUtil.isEmpty(ids))
            throw new EmptyStackException();
        User user = userAop.getUser();
        hxStorageMoveRecordParam.setDeptId(user.getDeptId());
        hxStorageMoveRecordParam.setTransferUserId(user.getUserId());
        hxStorageMoveRecordParam.setTransferUser(user.getName());
        hxStorageMoveRecordParam.setOperateId(user.getOperateId());
        String s = this.hxStorageMoveRecordService.removeStorage(hxStorageMoveRecordParam, ids);
        if (s.equals("error")) {
            return ResponseData.error("该库存有拣货任务未完成，无法移库");
        } else if (s.equals("error1")) {
            return ResponseData.error("查无此库存信息");
        } else if (s.equals("error2")) {
            return ResponseData.error("查无此目的库位");
        } else if (s.equals("error4")) {
            return ResponseData.error("不可以跨仓");

        } else if (s.equals("error5")) {
            return ResponseData.error("回传新零帮失败");
        } else if (s.equals("error10")) {
            return ResponseData.error("库位有其他库存无法移库");
        } else {
            return ResponseData.success(s);
        }
    }

    @ResponseBody
    @RequestMapping("/storageMoveCnt")
    public ResponseData storageMoveCnt(@RequestBody StorageMoveCntVo storageMoveCntVo) {
        if(ToolUtil.isEmpty(storageMoveCntVo.getMoveItem())){
            return ResponseData.error("请选择要转移的源库位及数量");
        }
        if(ToolUtil.isEmpty(storageMoveCntVo.getNewPositionId()) && ToolUtil.isEmpty(storageMoveCntVo.getNewPositionNumber())){
            return ResponseData.error("请选择目标库位");
        }
        return this.hxStorageMoveRecordService.storageMoveCnt(storageMoveCntVo);
    }

}


